/**
 * Created by zander on 2017/11/30.
 */

function getUserTask() {
    var localStorage = window.localStorage;
    var name = localStorage.getItem('name');
    $.ajax({
        type: "POST",
        url: "/getRunningJob",
        data: {username: name},
        dataType: "json",
        success: function (ret) {
            if (ret.status == "0000") {
                var jobdata = ret['content'];
                for (var i in jobdata) {
                    var jobName = jobdata[i]['jobname'];
                    var buildId = jobdata[i]['build_id'];
                    var sp = '<li style="height: 60px;" class="list-group-item" buildId = "' + buildId + '">' + jobName + '</li>';
                    var sp1 = '<li style="height: 60px" class="list-group-item"><div class="progress"><div id="progress' + i + '" class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width:1%">1%</div></div></li>';
                    $("#userTask").append(sp);
                    $("#taskJD").append(sp1);
                }
            } else {
                alert(ret.message);
            }
        },
        error: function () {
            alert("Error 500,请联系开发人员！");
        }
    });
}

function getExecResult() {
    var ilist = $("#userTask").find('li');
    var a = 0;
    for (var i = 0; i < ilist.length; i++) {
        var build_id = ilist[i].getAttribute('buildid');
        var jobname = ilist[i].innerHTML;
        $.ajax({
            type: "POST",
            url: "/getExecResult",
            async: false,
            data: {"jobname": jobname, "build_id": build_id},
            dataType: "json",
            success: function (ret) {
                if (ret.status == "0000") {
                    var progress = ret['content']['progress'];
                    $("#progress" + a).attr("style", "width:" + progress);
                    $("#progress" + a).text(progress);
                    a++;
                } else {
                    return alert(ret.message);
                }
            },
            error: function () {
                alert("Error 500,请联系开发人员！");
            }
        });
    }
}

function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}
window.onload = getUserTask();
window.setInterval("getExecResult()", 20000);